import { _decorator, Component, Node, Prefab, instantiate, Vec3} from 'cc';
import { Card } from "./Card";
import { CardManager } from './CardManager';

const { ccclass, property } = _decorator;

@ccclass('OutCardManage') // 用 @ccclass 声明类为 Cocos 组件
export class OutCardManager extends Component {

    @property(Prefab) cardPerfab = null;

	static instance: OutCardManager;

    onLoad() {
        OutCardManager.instance = this;
        console.log('OutCardManager onLoad!');
    }

    onReset() {
        // clear all card
        for(let i = 0; i < 4; i++)
        {
            const player = this.getPlayer(i);
            if(player)
            {
                player.removeAllChildren();
            }
        }
        
    }

    getPlayer(index:number) : Node| null {
        return this.node.getChildByName("mj"+index);
    }

    onOutCard(playerIndex:number, cardID: number = -1) {
        const player = this.getPlayer(playerIndex);
        if(player)
        {
            const perfab: Node | null = CardManager.createDeskCard(true);
            if(perfab)
            {
                const model: Node = perfab;
                const card = model.getComponent(Card);
                if(card)
                {
                    if(cardID != -1)
                    {
                        card.cardID = cardID;
                    }
                }

                const index = player.children.length;
                const maxCol = 6;
                const x = index % maxCol;
                const z = Math.floor(index / maxCol);
                model.position = new Vec3(x*0.03, 0, z*0.04);
                // model.eulerAngles = new Vec3(90, 180, 180);
                player.addChild(model);
            }

        }
    }


}